/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/24
 * Time: 上午11:44
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Core;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.cake21.uitest.System.Map.BrowserMap;
import com.cake21.uitest.System.Map.CaseMap;

public class Commander {
    /*名称*/
    private String name = Server.generateId();

    public String getNamex() {
        return name;
    }

    /*线程映射表*/
    private ConcurrentHashMap<Long, Browser> threadMap = new ConcurrentHashMap<Long, Browser>();

    public ConcurrentHashMap<Long, Browser> getThreadMap() {
        return threadMap;
    }

    /*判断映射表*/
    private ConcurrentHashMap<String, Judge> judgeMap = new ConcurrentHashMap<String, Judge>();

    public ConcurrentHashMap<String, Judge> getJudgeMap() {
        return judgeMap;
    }

    /*浏览器队列*/
    private ConcurrentLinkedQueue<String> browserQueue = initBrowserQueue();

    public ConcurrentLinkedQueue<String> getBrowserQueue() {
        return browserQueue;
    }

    /*初始化浏览器队列*/
    private ConcurrentLinkedQueue<String> initBrowserQueue() {
        ConcurrentLinkedQueue<String> browserQueue = new ConcurrentLinkedQueue<String>();
        browserQueue.addAll(BrowserMap.getMap().keySet());
        return browserQueue;
    }

    /*用例队列*/
    private ConcurrentLinkedQueue<String> caseQueue = initCaseQueue();

    public ConcurrentLinkedQueue<String> getCaseQueue() {
        return caseQueue;
    }

    /*初始化用例队列*/
    private ConcurrentLinkedQueue<String> initCaseQueue() {
        ConcurrentLinkedQueue<String> caseQueue = new ConcurrentLinkedQueue<String>();
        caseQueue.addAll(CaseMap.getMap().keySet());
        return caseQueue;
    }

    /*任务队列*/
    public void dispatch() {
        Iterator<String> browserIterator = browserQueue.iterator();
        if (Server.getRunMode().equals("remote") || Server.getRunMode().equals("multiple")) {
            /*并发测试线程池*/
            ExecutorService threadPool = Executors.newCachedThreadPool();
            while (browserIterator.hasNext()) {
                String browserKey = browserIterator.next().toString();
                Browser browser = new Browser().initial(browserKey);
                /*远程测试，并发启动多浏览器*/
                threadPool.execute(browser);
            }
            try {
				/*等待所有线程结束*/
                threadPool.shutdown();
                threadPool.awaitTermination(24, TimeUnit.HOURS);
            } catch (Exception e) {
				/*线程错误*/
                e.printStackTrace();
            }
            Report.save();
        } else if (Server.getRunMode().equals("local")) {
            while (browserIterator.hasNext()) {
                String browserKey = browserIterator.next().toString();
                Browser browser = new Browser().initial(browserKey);
				/*本地测试，按顺序启动多浏览器*/
                browser.launch();
            }
            Report.save();
        } else {
			/*模式未误别*/
        }
    }

}
